package org.bud.util.bit;

import org.bud.excel.expot.exception.UtilException;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author shaun
 * @date 2024/7/19
 * @instruction
 */
public abstract class Funcs {
    public static interface WrapSupplier<T>{
        T get() throws Exception;
    }

    public static interface WrapConsumer<T>{
        void accept(T t) throws Exception;
    }

    public static <T> T getOrThrow(WrapSupplier<T> sup){
        return getOrThrow(sup,true);
    }

    public static <T> T getOrThrow(WrapSupplier<T> sup,boolean isThrow){
        try {
            return sup.get();
        } catch (Exception e){
            if(isThrow){
                throw new UtilException(e);
            }
            return null;
        }
    }

    public static <T> void doOrThrow(WrapConsumer<T> con,T t){
        doOrThrow(con,t,true);
    }

    public static <T> void doOrThrow(WrapConsumer<T> con,T t,boolean isThrow){
        try {
            con.accept(t);
        } catch (Exception e){
            if(isThrow){
                throw new UtilException(e);
            }
        }
    }


}
